import { Controller, Post, Body, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { SampleDto } from './dto/sample.dto';
import { UploadService } from './upload.service';
import { Multer } from 'multer';
import { GoodsService } from '../goods/goods.service';
import { CreateGoodsDto } from '../goods/dto/create-goods.dto';

@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService,
              private readonly goodsService: GoodsService) {}
  @UseInterceptors(FileInterceptor('file'))
  @Post('file/save')
  async uploadFile(
    @Body() body: SampleDto,
    @UploadedFile() file: Multer.File,
  ) {
    const filePath = await this.uploadService.saveFile(file);
    const createGoodsDto=new CreateGoodsDto();
    createGoodsDto.businessId = body.businessId
    createGoodsDto.goodName = body.goodName
    createGoodsDto.price = body.price
    createGoodsDto.imgAddr = filePath.split('/').pop().split('\\').pop()
    console.log(createGoodsDto)
    await this.goodsService.create(createGoodsDto)
    return {
      body,
      filePath,
    };
  }
}
